﻿<?php
	#实际使用的就是这个（APP端快递查询，
//电商ID 请到快递鸟官网申请http://kdniao.com/reg
defined('EBusinessID') or define('EBusinessID', '1378396');
//电商加密私钥，快递鸟提供，注意保管，不要泄漏 请到快递鸟官网申请http://kdniao.com/reg
defined('AppKey') or define('AppKey', '602ce42b-17e5-4d20-8e45-7a0c98de11dd');
//请求url
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');

//调用查询物流轨迹
//---------------------------------------------

//---------------------------------------------
//  
/**
 * Json方式 查询订单物流轨迹
 */
function getOrderTracesByJson(){
	$bianma=$_REQUEST['bianma'];
	$danhao=$_REQUEST['danhao'];
	$requestData= "{'OrderCode':'','ShipperCode':'{$bianma}','LogisticCode':'{$danhao}'}";
	
	$datas = array(
        'EBusinessID' => EBusinessID,
        'RequestType' => '1002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = encrypt($requestData, AppKey);
	$result=sendPost(ReqURL, $datas);	
	
	//根据公司业务处理返回的信息......
	
	return $result;
}
 
/**
 *  post提交数据 
 * @param  string $url 请求Url
 * @param  array $datas 提交的数据 
 * @return url响应返回的html
 */
function sendPost($url, $datas) {
    $temps = array();	
    foreach ($datas as $key => $value) {
        $temps[] = sprintf('%s=%s', $key, $value);		
    }	
    $post_data = implode('&', $temps);
    $url_info = parse_url($url);
	if(empty($url_info['port']))
	{
		$url_info['port']=80;	
	}
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader.= "Host:" . $url_info['host'] . "\r\n";
    $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
    $httpheader.= "Connection:close\r\n\r\n";
    $httpheader.= $post_data;
    $fd = fsockopen($url_info['host'], $url_info['port']);
    fwrite($fd, $httpheader);
    $gets = "";
	$headerFlag = true;
	while (!feof($fd)) {
		if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
			break;
		}
	}
    while (!feof($fd)) {
		$gets.= fread($fd, 128);
    }
    fclose($fd);  
    
    return $gets;
}

/**
 * 电商Sign签名生成
 * @param data 内容   
 * @param appkey Appkey
 * @return DataSign签名
 */
function encrypt($data, $appkey) {
    return urlencode(base64_encode(md5($data.$appkey)));
}

//获取支持查询的快递公司列表
function getKuaiDiGongSiList(){

	$arr=array(

		'ZTO'=>'中通快递',
		'HTKY'=>'百世快递',
		'YTO'=>'圆通速递',
		'TNT'=>'TNT快递',
		'YD'=>'韵达速递',
		'STO'=>'申通快递',
		'YZPY'=>'邮政快递包裹',

		'JD'=>'京东快递',

		'UC'=>'优速快递',

		'EMS'=>'EMS',

		'DBL'=>'德邦快递',

		'ZJS'=>'宅急送',

		'ANEKY'=>'安能快运',

	);

	echo json_encode($arr);

}



//查询接口
function query(){
	$logisticResult=getOrderTracesByJson();
	echo $logisticResult;
}

//主要调用
if(isset($_REQUEST['f'])){
	$fun=$_REQUEST['f'];
	$fun();
}
?>